<script setup lang="ts">
import { onMounted } from 'vue'
import ConsultItem from './ConsultItem.vue'
import { ConsultType } from '@/enums'
import { getConsultOrderList } from '@/services'
import { ref } from 'vue'
import type { ConsultOrderItem, ConsultOrderListParams } from '@/types/consult'

// 接收问诊类型
defineProps<{
  type: ConsultType
}>()
const paramsObj = ref<ConsultOrderListParams>({
  current: 1,
  pageSize: 10,
  type: ConsultType.Fast
})
const list = ref<ConsultOrderItem[]>([])
// 上拉加载
const loading = ref(false)
const finished = ref(false)
const onLoad = async () => {
  const res = await getConsultOrderList(paramsObj.value)
  // 数据的累加
  list.value.push(...res.data.rows)
  // 页码累加
  paramsObj.value.current++
  // 加载状态结束
  loading.value = false
  // 数据全部做完( 只要是当前页码 大于 总页码 那么就显示加载更多)
  if (paramsObj.value.current > res.data.pageTotal) {
    finished.value = true
  }
}

//因为组件默认就会触发这个 onload 所以不需要写在 onMounted 中
// onMounted(() => {
//   onLoad()
// })

// 删除数据
const onDel = (id: string) => {
  list.value = list.value.filter((item) => item.id !== id)
}
</script>

<template>
  <div class="consult-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <consult-item @on-del="onDel" v-for="item in list" :key="item.id" :item="item"></consult-item>
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.consult-list {
  padding: 10px 15px;
}
</style>
